#include "../header.h"

int get_mynums(int numprocs, int myid) {
  int maxnum;
  
  maxnum = (int)xsteps; 
  if (myid < (maxnum-2)%numprocs) return ((maxnum-2)/numprocs)+1;
  else return (maxnum-2)/numprocs;
}

int get_fromnum(int numprocs, int myid) {
  int maxnum;

  maxnum = (int)xsteps;
  if (myid < (maxnum-2)%numprocs) return ((maxnum-2)/numprocs)*myid+myid+1;
  else return ((maxnum-2)/numprocs)*myid+(maxnum-2)%numprocs+1;
}

double ***InitializeArray(double ***M, int xlength) {
  int i,j,ylength;

  ylength = ysteps;
  M = (double ***)malloc(tsteps*sizeof(double*));
  for(i=0;i<tsteps;++i) {
    M[i] = (double **)malloc(ylength*sizeof(double*));
  }
  for(i=0;i<tsteps;++i) {
    for(j=0;j<ylength;++j) {
        M[i][j] = (double *)malloc((xlength+2)*sizeof(double));
    }
  }
  return M;
}

void SaveResultsToFile(double ***Predator,double ***Pray,int rank) {
  FILE *fp;
  int i;
  char *fname;

  fname = (char *)malloc(5*sizeof(char));
  sprintf(fname,"res%d",rank);

  if ((fp = fopen(fname,"w")) == NULL) {
    printf("Can\'t open file\n");  
  } 
  for(i=0;i<tsteps;++i) 
    fprintf(fp,"%.3f\t%.3f\n",Predator[i][1][1],Pray[i][1][1]);

  fclose(fp);
}

void Print2dArray(double **Array, int xmax, int ymax) {
  int i,j;

  for(i=0;i<ymax;++i) {
    for(j=0;j<xmax;++j) 
      printf("%.2f  ",Array[i][j]);
    printf("\n");
  }
  printf("\n");
}





